package com.ag.score.apicontroller;

import com.ag.common.api.R;
import com.ag.common.api.ResultCode;
import com.ag.score.apiservice.AppService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserApiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserApiController.class);
    @Autowired
    AppService appService;

    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = " 1失败 0 成功, 参数(例) {\"username\":\"123456\",\"pass\":\"123456\"   }")
    public R userLogin(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject paramJson){
        response.setHeader("Access-Control-Allow-Origin", "*");

        try{
            boolean flag = appService.userLogin(paramJson);
            if(flag) {
                return R.data(ResultCode.SUCCESS.getCode(), paramJson.get("resultStr").toString());
            }else{
                return R.fail(1,"登录验证失败");
            }

        }catch (Exception e){
            LOGGER.error("UserApiController userLogin error:"+e.getMessage());
            return R.fail(1, "发生异常，获取失败"+e.getMessage());
        }

    }


}
